package com.flying.arts.trade.domain.service;

import com.flying.arts.trade.domain.entity.Trade;
import com.flying.arts.trade.domain.repository.TradeRepository;
import com.flying.arts.common.domain.entity.Entity;
import com.flying.arts.common.domain.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service("buyTradeService")
public class TradeServiceImpl extends BaseService<Trade, Long> implements TradeService {
    private TradeRepository repository;

    @Autowired
    public TradeServiceImpl(TradeRepository repository) {
        super(repository);
        this.repository = repository;
    }

    @Override
    public void add(Trade trade) {
        if (trade.getId() == null) {
            throw new RuntimeException("Trade id cannot be null.");
        }
        if (repository.contains(trade.getId())) {
            throw new RuntimeException(String.format("There is already a product with the id - %s", trade.getId()));
        }
        super.add(trade);
    }

    @Override
    public void update(Trade trade) {
        repository.update(trade);
    }

    @Override
    public void delete(long id) {
        repository.remove(id);
    }

    @Override
    public Entity findById(long id) {
        return repository.get(id);
    }

    @Override
    public Collection<Trade> findByStockName(String name) {
        return repository.findByStockName(name);
    }
}
